<?php

if (!defined('BASEPATH'))
exit('No direct script access allowed');

class Research extends MX_Controller {

	/**
	 * Index Page for this controller.
	 *
	 * Maps to the following URL
	 * 		http://example.com/index.php/welcome
	 * 	- or -
	 * 		http://example.com/index.php/welcome/index
	 * 	- or -
	 * Since this controller is set as the default controller in
	 * config/routes.php, it's displayed at http://example.com/
	 *
	 * So any other public methods not prefixed with an underscore will
	 * map to /index.php/welcome/<method_name>
	 * @see http://codeigniter.com/user_guide/general/urls.html
	 */
	public function Research() {
		parent::__construct();
		$this->load->model('lookupmodel');
		$this->load->model('helpermodel');
		//check is logged in
		$this->load->model('languagemodel');
		$this->language = $this->languagemodel->is_language_using();

		$this->load->model('mdl_research');
		
		$this->load->model('banners/bannermodel');
	}

	public function index() {
		$this->lang->load('market/research', $this->config->item('language'));

		//SEO
		$data['title'] = $this->lang->line('research_text_title');
		$data['description'] = $this->lang->line('research_text_description');
		$data['keywords'] = $this->lang->line('research_text_keywords');

		$data['text_heading'] = $this->lang->line('text_heading');

		$data['breadcrumbs'] = array();

		$data['breadcrumbs'][] = array(
			'text'      => '',
			'href'      => base_url()
		);

		$data['breadcrumbs'][] = array(
			'text'      => $this->lang->line('text_heading'),
			'href'      => base_url() . 'research.html'
		);

		//Find countries
		$data['countries'] = $this->lookupmodel->getCountries($this->config->item('config_language_id'));

		//Find city
		$data['cities'] = $this->lookupmodel->getListCities($this->config->item('config_language_id'), $this->config->item('market'));

		if($this->input->get('country') != "") {
			$data['country'] = $this->input->get('country');
		} else {
			$data['country'] = NULL;
		}

		if($this->input->get('city') != "") {
			$data['city'] = $this->input->get('city');
		} else {
			$data['city'] = NULL;
		}

		if($this->input->get('businessLine') != "") {
			$data['businessLine'] = $this->input->get('businessLine');
		} else {
			$data['businessLine'] = NULL;
		}

		if($this->input->get('startDate') != "") {
			$data['startDate'] = $this->input->get('startDate');
		} else {
			$data['startDate'] = NULL;
		}

		if($this->input->get('endDate') != "") {
			$data['endDate'] = $this->input->get('endDate');
		} else {
			$data['endDate'] = NULL;
		}
		
		if($this->input->get('keyword') != "") {
			$data['keyword'] = $this->input->get('keyword');
			if($data['keyword'] == "Free text") {
				$data['keyword'] = NULL;
			}
		} else {
			$data['keyword'] = NULL;
		}

		if($this->input->get('action') != "") {
			$data['action'] = $this->input->get('action');
		} else {
			$data['action'] = NULL;
		}


		if ($this->input->get('page')) {
			$data['page'] = $this->input->get('page');
		}
		else{
			$data['page'] = 1;
		}

		if($this->input->get('limit')) {
			$limit = $this->input->get('limit');
		} else {
			$limit = PER_PAGE;
		}

		$data['limit'] = $limit;

		$data['offset'] = ($data['page'] - 1) * $limit;
		$data['limit'] = $limit + $data['offset'];

		$data['language'] = $this->config->item('config_language_id');
		$data['market'] = $this->config->item('market');
		$data['listing_id'] = NULL;
		 
		$data['researchCategories'] = $this->mdl_research->getResearchCategories($data['businessLine'], $this->config->item('config_language_id'), $this->config->item('market'));
		$data['categories'] = array();
		
		if($data['researchCategories']) {
			foreach ($data['researchCategories'] as $category) {
				$children_data = array();
				 
				$results = $this->mdl_research->getResearchUsingStored($category->ResearchTopicFID, $data, $this->config->item('config_language_id'), $this->config->item('market'));
				 
				foreach ($results as $child) {
					if(!empty($child['ImageUrl'])) {
						$imageUrl = $this->config->item('HTTP_ARTICLE_MEDIA_IMAGE') . $child['ImageUrl'];
					} else {
						$imageUrl = $this->config->item('HTTP_IMAGE') . 'photos/buyer-guide.png';
					}
	
					$children_data[] = array(
						'post_id' 		=> $child['Post_ID'],
						'name'        	=> $child['Title'],
						'sumarry'     	=> $child['Summary'],
						'content'     	=> $child['PostContent'],
						'image'			=> $imageUrl,
						'href'        	=> base_url() . 'research/detail/' . $child['Link']
					);
				}
	
				$data['categories'][] = array(
					'category_id' => $category->ResearchTopicFID,
					'name'        => $category->Name,
					'children'    => $children_data,
					'href'        => base_url() . 'research/' . $category->ResearchTopicFID
				);
			}
		}
		
		//get banner for right side
		$data['banner'] = $this->bannermodel->getBanners('News & Research', $this->config->item('market'), $this->config->item('config_language_id'));

		$data['module'] = "research";
		$data['view_file'] = "research";

		echo Modules::run('template/default_template', $data);
	}
	
	function detail($url = NULL) {
		$this->lang->load('market/research', $this->config->item('language'));
		//SEO
		$data['title'] = $this->lang->line('research_text_title');
		$data['description'] = $this->lang->line('research_text_description');
		$data['keywords'] = $this->lang->line('research_text_keywords');

		$data['text_heading'] = $this->lang->line('text_heading');

		$data['breadcrumbs'] = array();

		$data['breadcrumbs'][] = array(
			'text'      => '',
			'href'      => base_url()
		);

		$data['breadcrumbs'][] = array(
			'text'      => $this->lang->line('text_heading'),
			'href'      => base_url() . 'research.html'
		);
		
		$options = array();
		
		$parts = explode('-', $url);
        $id = end($parts);

		//$id = $this->input->get('id');

		if(!isset($id) || !is_numeric($id) || $id < 0) {
			redirect(base_url() . 'error_404', 'refresh');
		}
		
		//$listingtoview['media'] = $this->mdl_listingmapmodel->get_media(128, "Gallery", 20);
		
		//$thumb = $this->imagemodel->resize('3251.jpg', THUMB_IMAGE_WIDTH, THUMB_IMAGE_HEIGHT, HTTP_PROJECT_LISTING_IMAGE . 128 . '/', $this->config->item('HTTP_LISTING_MEDIA_IMAGE') . 128 . '/');

		$options['category_id'] = NULL;
		$options['country'] = NULL;
		$options['city'] = NULL;
		$options['startDate'] = NULL;
		$options['endDate'] = NULL;
		$options['keyword'] = NULL;
		$options['offset'] = NULL;
		$options['limit'] = NULL;
		$options['listing_id'] = $id;
		$options['language'] = $this->config->item('config_language_id');
		$options['market'] = $this->config->item('market');

		if ($this->input->get('page')) {
			$options['page'] = $this->input->get('page');
		} else {
			$options['page'] = 1;
		}

		$options['offset'] = NULL;
		$options['limit'] = NULL;

		//$results = $this->mdl_listingmapmodel->getListingWithWhere($options);
		$results = $this->mdl_research->getResearchUsingStored($options['category_id'], $options, $this->config->item('config_language_id'), $this->config->item('market'));
		
		$data['research_news'] = array();
		if(isset($results) && is_array($results) && count($results) > 0) {
			$data['research_news'] = $results[0];
			
			if(!empty($results[0]['ImageUrl'])) {
				$imageUrl = $this->config->item('HTTP_ARTICLE_MEDIA_IMAGE') . $results[0]['ImageUrl'];
			} else {
				$imageUrl = $this->config->item('HTTP_IMAGE') . 'photos/buyer-guide.png';
			}
			
			//download file
			if(!empty($results[0]['FileUrl'])) {
				$fileUrl = $this->config->item('HTTP_ARTICLE_MEDIA_IMAGE') . $results[0]['FileUrl'];
			} else {
				$fileUrl = false;
			}
			
			$data['research_news'] = array(
				'post_id'		=> $results[0]['Post_ID'],
				'title'			=> $results[0]['Title'],
				'content'		=> $results[0]['PostContent'],
				'image'			=> $imageUrl,
				'file'			=> $fileUrl
			);
			
			$data['title'] = $results[0]['Title'] . " | JLL Residential Indonesia";
			
		} else {
			redirect(base_url() . 'error_404', 'refresh');
		}

		$data['breadcrumbs'][] = array(
			'text'      => $results[0]['Title'],
			'href'      => base_url() . 'research/detail/' . $results[0]['Link']
		);
		
		//get banner for right side
		$data['banner'] = $this->bannermodel->getBanners('News & Research', $this->config->item('market'), $this->config->item('config_language_id'));
		
		$data['module'] = "research";
		$data['view_file'] = "research_detail";

		echo Modules::run('template/default_template', $data);
	}
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */